﻿namespace NumeralSystems
{
    using System;
    using System.Collections.Generic;

    class FromRandomNumSystemToRandom
    {
        // 1.1.8.13. Write a program that by given N, S, D (2 ≤ S, D ≤ 16) converts the number N from an S-based numeral
        // system to a D based numeral system.

        public static int CalculatePower(int num, int power)
        {
            int result = 1;
            for (int i = 0; i < power; i++)
            {
                result *= num;
            }
        
            return result;
        }

        static void Main()
        {
            int s = int.Parse(Console.ReadLine());
            int d = int.Parse(Console.ReadLine());
            string num = Console.ReadLine();
            int numAsDecimal = 0;
            for (int i = 0; i < num.Length; i++)
            {
                if (num[num.Length - 1 - i] == '0')
                {
                    numAsDecimal += 0 * CalculatePower(s, i);
                }
                else if (num[num.Length - 1 - i] == '1')
                {
                    numAsDecimal += 1 * CalculatePower(s, i);
                }
                else if (num[num.Length - 1 - i] == '2')
                {
                    numAsDecimal += 2 * CalculatePower(s, i);
                }
                else if (num[num.Length - 1 - i] == '3')
                {
                    numAsDecimal += 3 * CalculatePower(s, i);
                }
                else if (num[num.Length - 1 - i] == '4')
                {
                    numAsDecimal += 4 * CalculatePower(s, i);
                }
                else if (num[num.Length - 1 - i] == '5')
                {
                    numAsDecimal += 5 * CalculatePower(s, i);
                }
                else if (num[num.Length - 1 - i] == '6')
                {
                    numAsDecimal += 6 * CalculatePower(s, i);
                }
                else if (num[num.Length - 1 - i] == '7')
                {
                    numAsDecimal += 7 * CalculatePower(s, i);
                }
                else if (num[num.Length - 1 - i] == '8')
                {
                    numAsDecimal += 8 * CalculatePower(s, i);
                }
                else if (num[num.Length - 1 - i] == '9')
                {
                    numAsDecimal += 9 * CalculatePower(s, i);
                }
                else if (num[num.Length - 1 - i] == 'A')
                {
                    numAsDecimal += 10 * CalculatePower(s, i);
                }
                else if (num[num.Length - 1 - i] == 'B')
                {
                    numAsDecimal += 11 * CalculatePower(s, i);
                }
                else if (num[num.Length - 1 - i] == 'C')
                {
                    numAsDecimal += 12 * CalculatePower(s, i);
                }
                else if (num[num.Length - 1 - i] == 'D')
                {
                    numAsDecimal += 13 * CalculatePower(s, i);
                }
                else if (num[num.Length - 1 - i] == 'E')
                {
                    numAsDecimal += 14 * CalculatePower(s, i);
                }
                else if (num[num.Length - 1 - i] == 'F')
                {
                    numAsDecimal += 15 * CalculatePower(s, i);
                }
            }


            List<byte> numCompilation = new List<byte>();
            while (numAsDecimal != 0)
            {
                numCompilation.Add((byte)(numAsDecimal % d));
                numAsDecimal /= d;
            }

            numCompilation.Reverse();
            char[] result = new char[numCompilation.Count];

            for (int i = 0; i < result.GetLength(0); i++)
            {
                if (numCompilation[i] == 0)
                {
                    result[i] = '0';
                }
                else if (numCompilation[i] == 1)
                {
                    result[i] = '1';
                }
                else if (numCompilation[i] == 2)
                {
                    result[i] = '2';
                }
                else if (numCompilation[i] == 3)
                {
                    result[i] = '3';
                }
                else if (numCompilation[i] == 4)
                {
                    result[i] = '4';
                }
                else if (numCompilation[i] == 5)
                {
                    result[i] = '5';
                }
                else if (numCompilation[i] == 6)
                {
                    result[i] = '6';
                }
                else if (numCompilation[i] == 7)
                {
                    result[i] = '7';
                }
                else if (numCompilation[i] == 8)
                {
                    result[i] = '8';
                }
                else if (numCompilation[i] == 9)
                {
                    result[i] = '9';
                }
                else if (numCompilation[i] == 10)
                {
                    result[i] = 'A';
                }
                else if (numCompilation[i] == 11)
                {
                    result[i] = 'B';
                }
                else if (numCompilation[i] == 12)
                {
                    result[i] = 'C';
                }
                else if (numCompilation[i] == 13)
                {
                    result[i] = 'D';
                }
                else if (numCompilation[i] == 14)
                {
                    result[i] = 'E';
                }
                else if (numCompilation[i] == 15)
                {
                    result[i] = 'F';
                }
            }

            Console.WriteLine(string.Join("", result));
        }
    }
}